package com.ehome.poct.base.device.service;

import cn.hutool.core.util.ObjectUtil;
import com.ehome.core.page.Page;
import com.ehome.core.service.AbstractCrudServiceImpl;
import com.ehome.poct.base.api.dto.device.*;
import com.ehome.poct.base.api.virtual.TestDeviceVersionRelationService;
import com.ehome.poct.base.device.repository.TestDeviceRepository;
import com.ehome.poct.base.device.repository.TestDeviceVersionRelationRepository;
import com.ehome.poct.base.device.repository.TestDeviceVersionRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DecimalFormat;
import java.util.List;
import java.util.Map;

/**
 * 设备和版本关联表SerivceImlp
 *
 * @author yjm
 * @Date 2021-12-13 15:52
 */
@Service
public class TestDeviceVersionRelationSerivceImlp extends AbstractCrudServiceImpl<TestDeviceVersionRelationDTO> implements TestDeviceVersionRelationService {
    
    @Autowired
    private TestDeviceVersionRelationRepository repository;
    @Autowired
    private TestDeviceRepository testDeviceRepository;
    @Autowired
    private TestDeviceVersionRepository testDeviceVersionRepository;
    @Override
    public TestVersionDTO getVersionData(TestVersionRelationQry qry) {
        TestVersionDTO versionDTO = new TestVersionDTO();
    
        /**
         * 绑定设备数
         */
        Integer count = testDeviceRepository.bindDeviceCount(qry);
        
    
        /**
         * TOP1软件版本
         */
        String topOneVersionCode = repository.topOneVersion(qry);
    
        /**
         * TOP1软件版本数
         */
        Integer topOneBindDeviceCount = repository.topOneVersionCount(qry);
        if (ObjectUtil.isEmpty(topOneBindDeviceCount)){
            topOneBindDeviceCount = 0;
        }
    
        /**
         * 最低版本
         */
        String lowestVersionName = testDeviceVersionRepository.minVersion(qry.getPackageType(), qry.getDeviceType(), qry.getPackageId());
//        Integer lowestVersionCode = testDeviceVersionRepository.getCode(lowestVersionName);
        
        VersionQry versionQry = new VersionQry();
        versionQry.setDeviceType(qry.getDeviceType());
        versionQry.setPackageType(qry.getPackageType());
        versionQry.setPackageId(qry.getPackageId());
        versionQry.setVersion(lowestVersionName);
        /**
         * 最低数量
         */
        Integer lowestBindDeviceCount = repository.versionCount(versionQry);
        if (ObjectUtil.isEmpty(lowestBindDeviceCount)){
            lowestBindDeviceCount = 0;
        }
    
        /**
         * 最高版本
         */
        String highestVersionName = testDeviceVersionRepository.maxVersion(qry.getPackageType(), qry.getDeviceType(), qry.getPackageId());
//        Integer highestVersionCode = testDeviceVersionRepository.getCode(highestVersionName);
        VersionQry highestVersionQry = new VersionQry();
        highestVersionQry.setDeviceType(qry.getDeviceType());
        highestVersionQry.setPackageType(qry.getPackageType());
        highestVersionQry.setPackageId(qry.getPackageId());
        highestVersionQry.setVersion(highestVersionName);
        
        /**
         * 最高版本数量
         */
        Integer highestBindDeviceCount = repository.versionCount(highestVersionQry);
        if (ObjectUtil.isEmpty(highestBindDeviceCount)){
            lowestBindDeviceCount = 0;
        }
        
        DecimalFormat df = new DecimalFormat();
        df.setMaximumFractionDigits(2);
        df.setMinimumFractionDigits(2);
        
        /**
         * TOP1版本占比
         */
        String versionTopOne = "0.00%";
        if (ObjectUtil.isNotEmpty(versionTopOne) && topOneBindDeviceCount != 0){
            versionTopOne = df.format(topOneBindDeviceCount * 100.00 / count) + "%";
        }
        /**
         * 最低版本占比
         */
        String lowestVersion = "0.00%";
        if (ObjectUtil.isNotEmpty(lowestBindDeviceCount)){
            lowestVersion = df.format(lowestBindDeviceCount * 100.00 / count) + "%";
        }
        /**
         * 最高版本占比
         */
        String highestVersion = "0.00%";
        if (ObjectUtil.isNotEmpty(highestBindDeviceCount)){
            highestVersion = df.format(highestBindDeviceCount * 100.00 / count) + "%";
        }
        
        versionDTO.setVersionTopOne(versionTopOne);
        versionDTO.setTopOneVersionCode(topOneVersionCode);
        versionDTO.setTopOneBindDeviceCount(topOneBindDeviceCount);
        versionDTO.setBindDeviceCount(count);
        versionDTO.setLowestVersionCode(lowestVersionName);
        versionDTO.setLowestBindDeviceCount(lowestBindDeviceCount);
        versionDTO.setLowestVersion(lowestVersion);
        versionDTO.setHighestVersionCode(highestVersionName);
        versionDTO.setHighestBindDeviceCount(highestBindDeviceCount);
        versionDTO.setHighestVersion(highestVersion);
    
        return versionDTO;
    }
    
    @Override
    public Page<TestDeviceVersionDataDTO> pageVersionData(TestVersionRelationQry qry) {
                /**
         * 绑定设备数
         */
        Integer count = testDeviceRepository.bindDeviceCount(qry);

        /**
         * 版本占比
         */
        DecimalFormat df = new DecimalFormat();
        df.setMaximumFractionDigits(2);
        df.setMinimumFractionDigits(2);
        

        /**
         * 软件版本占比集合
         */
        List<TestDeviceVersionDataDTO> versionDataDTOS = repository.getDeviceVersionData(qry);

        /**
         * 求出占比
         */
        for (TestDeviceVersionDataDTO dto : versionDataDTOS) {
            String versionPercentage = df.format(dto.getVersionCount() * 100.00 / count) + "%";
            dto.setVersionPercentage(versionPercentage);
        }

        Page<TestDeviceVersionDataDTO> page = new Page<TestDeviceVersionDataDTO>();
        page.setList(versionDataDTOS);
        page.setCurrent(qry.getPageIndex());
        page.setSize(qry.getPageSize());
        page.setTotal(versionDataDTOS.size());
        return page;
    }

    @Override
    protected void preInsert(TestDeviceVersionRelationDTO dto, Map<String, Object> map) {
        TestDeviceDTO tdd = new TestDeviceDTO();
        tdd.setSno(dto.getSno());
        TestDeviceDTO testDeviceDTO = testDeviceRepository.selectOne(tdd);
        dto.setDeviceId(testDeviceDTO.getTestDeviceId());
    }
}